function getLibrary() {

    if (library!=null) {
        return (library);
    }
    // else load it up!
    var libraryLoader = new sp.Loader();
    var libraryURL = "assets/bumpz.swf"
    var libraryURLRequest = new sp.URLRequest(libraryURL);
    libraryLoader.load(libraryURLRequest);
    libraryLoader.contentLoaderInfo.addEventListener(sp.Event.COMPLETE, libraryLoaded);
    function libraryLoaded(event)
    {
        library = libraryLoader.contentLoaderInfo.applicationDomain;
    }
    return(library);
}

function getObj(img) {
    getLibrary();

    //TODO - better load timing
    if (library==null) {
        console.log("waiting for library to load... didnt load:" + img);
        return;
    }

    var ObjClass = library.getDefinition(img);
    if(ObjClass==null) {
        console.log("couldnt find linkage:" + img);
    }
    var obj = new ObjClass();
    return obj;
//    stage.addChild( greenRect );

}


function Box(x, y, width, height, img) {
    if (img==null) {
        img = '/assets/star.png'
    }

    this.fixture = new b2FixtureDef();
    this.fixture.density = 1.0;
    this.fixture.friction = 0.5;
    this.fixture.restitution = 0.0;
    this.fixture.shape = new b2PolygonShape();
    this.fixture.shape.SetAsBox(width / 2, height / 2);

    this.body = new b2BodyDef();
//				this.body.type = b2Body.b2_staticBody;
    this.body.position.x = x;
    this.body.position.y = y;

    /*
    var loader = new sp.Loader();
    loader.x = -30;
    loader.y = -30;

    loader.contentLoaderInfo.addEventListener(sp.Event.COMPLETE, function(event) {
        if(loader.content instanceof sp.Bitmap)
            loader.content.smoothing = true;

        // Cleanup
        event.target.removeEventListener(event.type, arguments.callee);
    });

    loader.load(new sp.URLRequest(img));

    this.view = new sp.Sprite();
    this.view.addChild( getObj("player") );
*/

    this.world = null;
}


function Player(x,y,width,height,img) {
    this.fixture = new b2FixtureDef();
    this.fixture.density = 1.0;
    this.fixture.friction = 0.5;
    this.fixture.restitution = 0.0;
    this.fixture.shape = new b2PolygonShape();
    this.fixture.shape.SetAsBox(width / 2, height / 2);

    this.body = new b2BodyDef();
    this.body.position.x = x;
    this.body.position.y = y;

    if (true) {
        var loader = new sp.Loader();
        loader.x = -30;
        loader.y = -30;

        loader.contentLoaderInfo.addEventListener(sp.Event.COMPLETE, function(event) {
            if(loader.content instanceof sp.Bitmap)
                loader.content.smoothing = true;

            // Cleanup
            event.target.removeEventListener(event.type, arguments.callee);
        });

        loader.load(new sp.URLRequest(img));
        this.view = new sp.Sprite();
        this.view.addChild(loader);

    } else {
        this.view = new sp.Sprite();
        obj = getObj("player");
        this.view.addChild(obj);
    }


    this.world = null;

}

function addPlayer(x, y) {
    var box = new Box(x, y, 60 / SCALE, 60 / SCALE);
    box.body.type = b2Body.b2_dynamicBody;

    var result = {body: box.addToWorld(world), view: box.view};

//    box.view.addEventListener(sp.TouchEvent.TOUCH_BEGIN, removeBox);
//    box.view.addEventListener(sp.MouseEvent.MOUSE_DOWN, removeBox);

    stage.addChild(box.view);
    boxes.push(result);
    
    return(result);
}

function build() {
    addPlayer(100,100);
}
